package com.ctsi.auth.integration.authenticator.impl;

import com.ctsi.auth.integration.IntegrationAuthentication;
import com.ctsi.auth.integration.authenticator.AbstractPreparableIntegrationAuthenticator;
import com.ctsi.organise.common.model.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author zhangjw
 * @className SmsIntegrationAuthenticator
 * @description 短信登陆认证
 * @date 2019/2/21 14:29
 **/
@Component
public class SmsIntegrationAuthenticator extends AbstractPreparableIntegrationAuthenticator {
    /**
     * 认证类型
     */
    private final static String SMS_AUTH_TYPE = "sms";

    @Autowired
    private PasswordEncoder passwordEncoder;

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public UserLogin authenticate(IntegrationAuthentication integrationAuthentication) {

//        //获取密码，实际值是验证码
//        String password = integrationAuthentication.getAuthParameter("password");
//        //获取用户名，实际值是手机号
//        String username = integrationAuthentication.getUsername();
//        //发布事件，可以监听事件进行自动注册用户
//        this.applicationEventPublisher.publishEvent(new SmsAuthenticateBeforeEvent(integrationAuthentication));
//        //通过手机号码查询用户
//        SysUserAuthentication sysUserAuthentication = this.sysUserClient.findUserByPhoneNumber(username);
//        if (sysUserAuthentication != null) {
//            //将密码设置为验证码
//            sysUserAuthentication.setPassword(passwordEncoder.encode(password));
//            //发布事件，可以监听事件进行消息通知
//            this.applicationEventPublisher.publishEvent(new SmsAuthenticateSuccessEvent(integrationAuthentication));
//        }
        return null;
    }

    @Override
    public void prepare(IntegrationAuthentication integrationAuthentication) {
//        String smsToken = integrationAuthentication.getAuthParameter("sms_token");
//        String smsCode = integrationAuthentication.getAuthParameter("password");
//        String username = integrationAuthentication.getAuthParameter("username");
//        Result<Boolean> result = verificationCodeClient.validate(smsToken, smsCode, username);
//        if (!result.getData()) {
//            throw new OAuth2Exception("验证码错误或已过期");
//        }
    }

    @Override
    public boolean support(IntegrationAuthentication integrationAuthentication) {
        return SMS_AUTH_TYPE.equals(integrationAuthentication.getAuthType());
    }

}
